 package com.txby.platformApi.component.service;

import java.util.Date;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.jck.mybatis.mapper.MybatisBaseDaoCRUD;
import com.txby.pf.common.utils.R;
import com.txby.platformCore.component.service.GlobalLogAspectInterceptor;
import com.txby.platformCore.component.util.Chinese2PinyinUtil;
import com.txby.platformCore.entity.shop.BaseGoods;
import com.txby.platformCore.entity.shop.GoodsPic;
import com.txby.platformCore.entity.shop.GoodsPicRealation;
import com.txby.platformCore.entity.shop.GoodsStockNum;


@Service
public class WebBaseGoodsService extends MybatisBaseDaoCRUD {

	@Resource
	private GlobalLogAspectInterceptor gla;
	@Resource(name = "mybatisBaseDaoCRUD")
	public MybatisBaseDaoCRUD mBaseDao;
	/**
	 * 新增普通商品
	 * @param BaseGoods
	 * @return
	 */
	@Transactional(propagation = Propagation.REQUIRED,timeout=36000,rollbackFor=Exception.class)
	public R saveBaseGoods(Map m,String userId)  {
		try {
			
			BaseGoods goods = getBaseGoods(m,userId);
			String stockNum = m.get("stockNum") == null ? "":m.get("stockNum").toString();
			String url = m.get("url") == null ? "":m.get("url").toString();
			goods.setIsDown(0);
			goods.setPublishTime(new Date());
			String sql="select * from "+mBaseDao.tableName(BaseGoods.class)+" where name='"+goods.getName()+"' and shopsId='"+goods.getShopsId()+"' and goodsTypeId='"+goods.getGoodsTypeId()+"'";
			BaseGoods old=(BaseGoods) mBaseDao.findBySqlUniqueObject(sql, BaseGoods.class, null);
			 if(old!=null){
				 return R.ok().error("该商品已存在");
			 }
			saveObject(goods);
			GoodsPic goodsPic = new GoodsPic();
			goodsPic.setUrl(url);
			saveObject(goodsPic);
			GoodsPicRealation goodsPicRealation = new GoodsPicRealation();
			goodsPicRealation.setGoodsId(goods.getId_());
			goodsPicRealation.setGoodsPicId(goodsPic.getId_());
			goodsPicRealation.setGoodsTypeId(4l);
			saveObject(goodsPicRealation);
			GoodsStockNum goodsStockNum = new GoodsStockNum();
			goodsStockNum.setGoodsTypeId(4l);
			goodsStockNum.setGoodsId(goods.getId_());
			goodsStockNum.setShopsId(goods.getShopsId());
			goodsStockNum.setStockNum(Long.valueOf(stockNum));
			goodsStockNum.setResidueNum(Long.valueOf(stockNum));
			goodsStockNum.setOrdersOutNum(0L);
			saveObject(goodsStockNum);
			
		} catch (Exception e) {
			return R.error(5000,e.getMessage());
		}
		return R.ok();
	}
	@Transactional(propagation = Propagation.REQUIRED,timeout=36000,rollbackFor=Exception.class)
	public R updateBaseGoods(Map m, String userId) {
		m.put("userId", userId);
		BaseGoods goods = getBaseGoods(m,userId);
		String stockNum = m.get("stockNum") == null ? "":m.get("stockNum").toString();
		String url = m.get("url") == null ? "":m.get("url").toString();
		Long id_=Long.valueOf(m.get("id_")+"");
		Long stockId=Long.valueOf(m.get("stockId")+"");
		Long goodsPicId=Long.valueOf(m.get("goodsPicId")+"");
		String bgSql="update t_txby_sp_baseGoods set updater=#{pojo.userId},updateTime=now(),name=#{pojo.name},keyName=#{pojo.keyName},"
				+ "spellFullName=#{pojo.spellFullName},spellSimpleName=#{pojo.spellSimpleName},introState=#{pojo.introState},unit=#{pojo.unit},"
				+ "introDetail=#{pojo.introDetail},introNotice=#{pojo.introNotice},price=#{pojo.price} where id_=#{pojo.id_}";
		mBaseDao.executeSql(bgSql, m);
		
		String stockSql="update t_txby_sp_goodsStockNum set updater=#{pojo.userId},updateTime=now(),"
				+ "stockNum=#{pojo.stockNum},residueNum=#{pojo.residueNum} where id_=#{pojo.stockId}";
		mBaseDao.executeSql(stockSql, m);
		
		String picSql="update t_txby_sp_goodsPic set updater=#{pojo.userId},updateTime=now(),"
					+ "url=#{pojo.url} where id_=#{pojo.goodsPicId}";
		mBaseDao.executeSql(picSql, m);
		return R.ok();
	}

	private BaseGoods getBaseGoods(Map m,String userId) {
		BaseGoods goods = new BaseGoods();
		goods.setCreater(userId);
		goods.setUpdater(userId);
		goods.setCreateTime(new Date());
		goods.setUpdateTime(new Date());
		goods.setStatus(0);
		goods.setGoodsTypeId(4L);
		String name = m.get("name") == null ? "":m.get("name").toString();
		String spellFullName = Chinese2PinyinUtil.getPingYin(name);
		String spellSimpleName = Chinese2PinyinUtil.getPinYinHeadChar(name);
		goods.setName(name);
		goods.setSpellFullName(spellFullName);
		goods.setSpellSimpleName(spellSimpleName);
		String NO = m.get("NO") == null ? "":m.get("NO").toString();
		goods.setNO(NO);
		String keyName = m.get("keyName") == null ? "":m.get("keyName").toString();
		goods.setKeyName(keyName);
		String introState = m.get("introState") == null ? "":m.get("introState").toString();
		goods.setIntroState(introState);
		String introDetail = m.get("introDetail") == null ? "":m.get("introDetail").toString();
		goods.setIntroDetail(introDetail);
		String introNotice = m.get("introNotice") == null ? "":m.get("introNotice").toString();
		goods.setIntroNotice(introNotice);

		String price = m.get("price") == null ? "":m.get("price").toString();
		goods.setPrice(Double.valueOf(price));
		
		String shopsId = m.get("shopsId") == null ? "":m.get("shopsId").toString();
		goods.setShopsId(Long.valueOf(shopsId));
		
		String unit = m.get("unit") == null ? "":m.get("unit").toString();
		goods.setUnit(unit);
		return goods;
	}
}
